package com.yanqu.road.dao.impl.wingroom.chakra;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.UserWingRoomData;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakra;
import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakraDetail;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WingRoomChakraDaoImpl extends TempDataObjectDao<UserWingRoomChakra> {

    private static String insertSql = "INSERT INTO `t_u_wingroom_chakra`(`userId`, `windWater`, `windWaterLimit`, `windWaterLevel`, `chakraExp`, `chakraLevel`, `waitChakraDetail`, `dailyVisitData`, `lastClearVisitTime`, `createTime`, `drawNum`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(UserWingRoomChakra data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWindWater()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWindWaterLimit()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWindWaterLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getChakraExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChakraLevel()));
        String waitChakraDetailStr = "";
        if (data.getWaitChakraDetail() != null && !data.getWaitChakraDetail().isEmpty()) {
            waitChakraDetailStr = JSONObject.toJSONString(data.getWaitChakraDetail());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, waitChakraDetailStr));
        String dailyVisitDataStr = "";
        if (!data.getDailyVisitData().isEmpty()) {
            dailyVisitDataStr = JSONObject.toJSONString(data.getDailyVisitData());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, dailyVisitDataStr));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastClearVisitTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawNum()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_u_wingroom_chakra` SET `windWater` = ?, `windWaterLimit` = ?, `windWaterLevel` = ?, `chakraExp` = ?, `chakraLevel` = ?, `waitChakraDetail` = ?, `dailyVisitData` = ?, `lastClearVisitTime` = ?, `drawNum` = ?, `updateTime` = ? WHERE `userId` = ?";
    @Override
    protected boolean updateData(UserWingRoomChakra data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getWindWater()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getWindWaterLimit()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWindWaterLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getChakraExp()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChakraLevel()));
        String waitChakraDetailStr = "";
        if (data.getWaitChakraDetail() != null && !data.getWaitChakraDetail().isEmpty()) {
            waitChakraDetailStr = JSONObject.toJSONString(data.getWaitChakraDetail());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, waitChakraDetailStr));
        String dailyVisitDataStr = "";
        if (!data.getDailyVisitData().isEmpty()) {
            dailyVisitDataStr = JSONObject.toJSONString(data.getDailyVisitData());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, dailyVisitDataStr));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastClearVisitTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWingRoomChakra parseData(ResultSet rs) throws SQLException {
        UserWingRoomChakra data = new UserWingRoomChakra();
        data.setUserId(rs.getLong("userId"));
        data.setWindWater(rs.getLong("windWater"));
        data.setWindWaterLimit(rs.getLong("windWaterLimit"));
        data.setWindWaterLevel(rs.getInt("windWaterLevel"));
        data.setChakraLevel(rs.getInt("chakraLevel"));
        data.setChakraExp(rs.getLong("chakraExp"));
        if (!StringUtils.isNullOrEmpty(rs.getString("waitChakraDetail"))) {
            List<UserWingRoomChakraDetail> list = new ArrayList<>();
            //兼容一下旧版本数据
            try {
                list = JSONObject.parseArray(rs.getString("waitChakraDetail"), UserWingRoomChakraDetail.class);
            } catch (Exception e) {
                try {
                    UserWingRoomChakraDetail userWingRoomChakraDetail = JSONObject.parseObject(rs.getString("waitChakraDetail"), UserWingRoomChakraDetail.class);
                    list.add(userWingRoomChakraDetail);
                } catch (Exception exception) {
                    logger.error("命盘待处理装备解析错误:userId:{}", data.getUserId());
                }
            }
            data.setWaitChakraDetail(list);
        }
        if (!StringUtils.isNullOrEmpty(rs.getString("dailyVisitData"))) {
            Map<String, Integer> visitNumMap = StringUtils.jsonToMap(rs.getString("dailyVisitData"));
            data.setDailyVisitData(visitNumMap);
        }
        data.setLastClearVisitTime(rs.getLong("lastClearVisitTime"));
        data.setDrawNum(rs.getInt("drawNum"));
        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }

    /**
     * 获取命盘基础数据
     * @param userId
     * @return
     */
    public UserWingRoomChakra getUserWingRoomChakra(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        UserWingRoomChakra data = null;
        String sql = "select * from t_u_wingroom_chakra where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                data = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }
}
